package com.tadpole.music.view.widget;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.tadpole.music.R;
import com.tadpole.music.adapter.base.BaseAdapter;
import com.tadpole.music.adapter.screen.ScreenCollegeAdapter;
import com.tadpole.music.adapter.screen.ScreenRankAdapter;
import com.tadpole.music.bean.question.QuestionRankBean;
import com.tadpole.music.bean.question.UniversityBean;
import com.tadpole.music.config.Constant;
import com.tadpole.music.iView.question.QuestionRankIView;
import com.tadpole.music.iView.question.UniversityIView;
import com.tadpole.music.presenter.question.QuestionRankPresenter;
import com.tadpole.music.presenter.question.UniversityPresenter;
import com.tadpole.music.utils.NoDoubleClickListener;
import com.tadpole.music.utils.SpUtil;
import com.tadpole.music.view.activity.login.LoginActivity;
import java.util.List;


/**
 * Created By 孙岩
 * on 2020/9/12
 */
public class StudentScreenDialog extends BaseDialog implements UniversityIView, QuestionRankIView {
    private TextView tvSubmit;
    //学院
    private RecyclerView rvCollege;
    private ScreenCollegeAdapter screenCollegeAdapter;
    //等级
    private RecyclerView rvRank;
    private ScreenRankAdapter screenRankAdapter;

    //获取学院的presenter
    private UniversityPresenter universityPresenter;
    //等级 presenter
    private QuestionRankPresenter questionRankPresenter;
    private IScreen iScreen;
    private Activity activity;

    //数据
    private int college_id;
    private int rank_id;

    private String college_name;

    public StudentScreenDialog(@NonNull Context context) {
        super(context);
    }

    public StudentScreenDialog(@NonNull Activity activity, int themeResId) {
        super(activity, themeResId);
        this.activity = activity;
        initView();
    }

    protected StudentScreenDialog(@NonNull Context context, boolean cancelable, @Nullable OnCancelListener cancelListener) {
        super(context, cancelable, cancelListener);
    }

    private void initView() {
        LayoutInflater inflater = (LayoutInflater) getContext()
                .getSystemService(getOwnerActivity().LAYOUT_INFLATER_SERVICE);
        View view = inflater.inflate(R.layout.student_screen_dialog,
                null);
        tvSubmit = view.findViewById(R.id.tvSubmit);
        rvCollege = view.findViewById(R.id.rvCollege);
        rvRank = view.findViewById(R.id.rvRank);

        setContentView(view);
        getWindow().setGravity(Gravity.BOTTOM);
        WindowManager.LayoutParams params = getWindow().getAttributes();
        params.width = WindowManager.LayoutParams.MATCH_PARENT;
        params.height = WindowManager.LayoutParams.WRAP_CONTENT;
        getWindow().setAttributes(params);

        //获取学院
        universityPresenter = new UniversityPresenter();
        universityPresenter.attachView(this);
        universityPresenter.getUniversity();


        //获取等级
        questionRankPresenter = new QuestionRankPresenter();
        questionRankPresenter.attachView(this);

        initListeners();
    }


    //学院回调
    @Override
    public void showUniversity(final List<UniversityBean.DataBean> collegeList) {
        if (collegeList != null && collegeList.size() != 0) {
            college_id = collegeList.get(0).getId();
            college_name = collegeList.get(0).getName();
            questionRankPresenter.getQuestionRank(collegeList.get(0).getId());

            //学院
            collegeList.get(0).setSelect(true);
            GridLayoutManager layoutManager = new GridLayoutManager(activity, 2);
            rvCollege.setLayoutManager(layoutManager);
            //创建适配器
            screenCollegeAdapter = new ScreenCollegeAdapter(activity, collegeList);
            //给RecyclerView设置适配器
            rvCollege.setAdapter(screenCollegeAdapter);
            rvCollege.setNestedScrollingEnabled(false);

            screenCollegeAdapter.setOnItemClickListener(new BaseAdapter.OnItemClickListener() {
                @Override
                public void onItemClick(View v, int position) {
                    for (int i = 0; i < collegeList.size(); i++) {
                        if (position == i) {
                            collegeList.get(i).setSelect(true);
                        } else {
                            collegeList.get(i).setSelect(false);
                        }
                    }
                    screenCollegeAdapter.notifyDataSetChanged();

                    questionRankPresenter.getQuestionRank(collegeList.get(position).getId());
                    college_id = collegeList.get(position).getId();
                    college_name = collegeList.get(position).getName();
                }
            });
        }
    }


    //等级回调
    @Override
    public void showQuestionRank(final List<QuestionRankBean.DataBean> rankList) {
        if (rankList != null && rankList.size() != 0) {

            rank_id = rankList.get(0).getId();
            //等级
            rankList.get(0).setSelect(true);
            GridLayoutManager layoutManager2 = new GridLayoutManager(activity, 3);
            rvRank.setLayoutManager(layoutManager2);
            //创建适配器
            screenRankAdapter = new ScreenRankAdapter(activity, rankList);
            //给RecyclerView设置适配器
            rvRank.setAdapter(screenRankAdapter);
            rvRank.setNestedScrollingEnabled(false);

            screenRankAdapter.setOnItemClickListener(new BaseAdapter.OnItemClickListener() {
                @Override
                public void onItemClick(View v, int position) {
                    for (int i = 0; i < rankList.size(); i++) {
                        if (position == i) {
                            rankList.get(i).setSelect(true);
                        } else {
                            rankList.get(i).setSelect(false);
                        }
                    }
                    screenRankAdapter.notifyDataSetChanged();
                    rank_id = rankList.get(position).getId();

                }
            });
        }
    }


    @Override
    public void show401() {
        SpUtil.getInstance(activity).getString(Constant.TOKEN, "");
        Intent intent = new Intent(activity, LoginActivity.class);
        activity.startActivity(intent);
    }

    //初始化监听器
    private void initListeners() {
        //提交
        tvSubmit.setOnClickListener(new NoDoubleClickListener() {
            @Override
            protected void onNoDoubleClick(View v) {
                iScreen.getScreenMode(1,college_id,college_name,rank_id);
            }
        });
    }

    public void setScreenListener(IScreen iScreen) {
        this.iScreen = iScreen;
    }

    @Override
    public void showLoading() {

    }

    @Override
    public void hideLoading() {

    }

    @Override
    public void showToast(String msg) {

    }

    @Override
    public void showErr() {

    }

    public interface IScreen {
        void getScreenMode(int mode, int college_id, String college_name, int rank_id);
    }
}
